<template>
  <c-view>
    <template v-slot:header>
      <div class="title">
        {{ $route.meta.title }}
      </div>
    </template>
    <div class="main__box">
      <c-table
        ref="cTable"
        selection
        hasBorder
        :max-height="maxHeight"
        :size="size"
        :loading="isLoading"
        :table-header="tableHeader"
        :table-list="tableList"
        :table-inner-btns="tableInnerBtns"
        :page-info="pageInfo"
        @change-pagination="changePagination"
      >
        <template v-slot:header>
          <c-search :form-model="searchObj" :form-items="searchItems" @submit-form="searchSubmit" @reset-form="searchReset"></c-search>
        </template>
      </c-table>
    </div>
    <div v-if="dialogObj.isShow">
      <c-dialog
        class="dialog-box"
        :is-show="dialogObj.isShow"
        :title="dialogObj.title"
        close-btn
        @before-close="dialogObj.isShow = false"
        @on-submit="dialogConfirm"
      >
        <bind-add ref="childRef" v-if="dialogObj.showType === 1" :init-data.sync="dialogObj.initData"></bind-add>
        <review-car ref="childRef" v-if="dialogObj.showType === 2" :init-data.sync="dialogObj.initData"></review-car>
      </c-dialog>
    </div>
  </c-view>
</template>

<script>
import mixinTable from 'mixins/table'
import CDialog from 'components/dialog'
import BindAdd from './add'
import ReviewCar from './review-car'

export default {
  name: 'owner',
  mixins: [mixinTable],
  components: {
    CDialog,
    BindAdd,
    ReviewCar
  },
  data(vm) {
    return {
      // 对话框对象
      dialogObj: {},
      // 表格内操作按钮
      tableInnerBtns: [
        {
          name: '绑定车辆',
          icon: 'el-icon-connection',
          handle(row) {
            vm.showDialog({
              initData: {
                id: row.id
              },
              showType: 1,
              title: `给用户(${row.phone})绑定车辆`
            })
          }
        },
        {
          name: '查看车辆',
          icon: 'el-icon-view',
          handle(row) {
            vm.showDialog({
              initData: {
                id: row.id
              },
              showType: 2,
              title: '查看车辆信息'
            })
          }
        }
      ],
      tableHeader: [
        {
          label: '手机号',
          prop: 'phone',
          width: 100,
          fixed: true,
          search: {
            type: 'input'
          }
        },
        {
          label: '头像',
          prop: 'portraitUrl',
          isImage: true
        },
        {
          label: '昵称',
          prop: 'nickName',
          width: 100
        },
        {
          label: '创建时间',
          prop: 'createTime'
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    /*
     * 查询表格列表数据
     */
    fetchData() {
      const { pageNo, pageSize } = this.pageInfo
      const { dateTime, ...other } = this.searchObj
      const searchDate = this.getSearchDate(dateTime, 'dateTime')
      this.isLoading = true
      this.$api.owner
        .fetch('queryOwnerPeopleList', {
          ...searchDate,
          ...other,
          pageNo,
          pageSize
        })
        .then(res => {
          this.isLoading = false
          if (res && res.totalCount) {
            const { data, totalCount } = res
            this.pageInfo.totalNum = totalCount
            this.tableList = data || []
          } else {
            this.tableList = res || []
          }
        })
    },
    /**
     * dialog对话框数据处理
     * @opts {*}
     */
    showDialog(opts) {
      this.dialogObj = {
        showType: opts.showType || 1,
        isShow: true,
        title: opts.title,
        initData: opts.initData
      }
    },
    /**
     * dialog新增、编辑处理函数
     */
    dialogConfirm() {
      const childRef = this.$refs.childRef
      childRef.$refs.formRef.validate(valid => {
        if (valid) {
          const params = childRef.formModel
          this.$api.owner.fetch('ownerPeopleBingCar', params).then(() => {
            this.responeHandle('绑定车辆成功')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
